﻿Public Class frmUuTien

    

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub frmUuTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        datDTUT.DataSource = clsUuTien.DSUT
    End Sub
    Private Sub PhucHoiNutSua()
        If btnSua.Text = "Lưu lại" Then
            btnSua.Text = "Sửa"
        End If
    End Sub

    Private Sub btnReLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        datDTUT.DataSource = clsUuTien.DSUT
        txtTimKiem.Text = ""
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiNutSua()
        clsUuTien.Them(txtMaDTUT.Text, txtTenDTUT.Text)
        datDTUT.DataSource = clsUuTien.DSUT
        btnReLoad_Click(sender, e)
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        PhucHoiNutSua()
        datDTUT.DataSource = clsUuTien.TimKiem(txtTimKiem.Text)
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "Sửa" Then
            txtMaDTUT.Text = datDTUT.CurrentRow.Cells("MaDTUT").Value
            txtTenDTUT.Text = datDTUT.CurrentRow.Cells("TenDTUT").Value
            btnSua.Text = "Lưu lại"
        Else
            btnSua.Text = "Sửa"
            clsUuTien.Sua(txtMaDTUT.Text, txtTenDTUT.Text)
            btnReLoad_Click(sender, e)

        End If
    End Sub

    Private Sub datDTUT_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datDTUT.CellDoubleClick
        PhucHoiNutSua()
        btnSua_Click(sender, e)
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiNutSua()
        If MsgBox("Bạn có muốn xóa đối tượng có mã ưu tiên là '" & txtMaDTUT.Text & "' đi không ?", MsgBoxStyle.YesNo, "Thông báo") = MsgBoxResult.Yes Then
            'clsUuTien.Xoa(txtMaDTUT.Text)
            'datDTUT.DataSource = clsUuTien.DSUT
            If clsUuTien.Xoa(txtMaDTUT.Text) = False Then
                MsgBox("Không thể xóa đối tượng ưu tiên có mã " & txtMaDTUT.Text & " vì còn tồn tại độc giả thuộc loại đối tượng ưu tiên này.", MsgBoxStyle.Critical, "Lỗi")
            End If
            datDTUT.DataSource = clsUuTien.DSUT
        End If

    End Sub

    Private Sub btnLamLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamLai.Click
        PhucHoiNutSua()
        txtMaDTUT.Text = ""
        txtTenDTUT.Text = ""

    End Sub
End Class